// pages/userWorks/userWorks.js
import { proxy } from '../../utils/util.js';
import LoginStore from '../login/store.js';
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    navbarData: {
      showCapsule: 1, //是否显示左上角图标   1表示显示    0表示不显示
      title: '', //导航栏 中间的标题
      background: 'linear-gradient(to bottom, rgba(0, 0, 0, 0.7), rgba(0, 0, 0, 0))',
      hasFilter: true,
      color: '#FFFFFF',
      btnStyle: 'white', //black, white
    },
    navBarHeight: app.globalData.height,
    params: {},
    urlStr: '',
    userId: '',
    curWorksType: 'profileList'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      userId: options.userId,
      curWorksType: options.tab
    }, () => {
      this.setTabs()
    })
  },

  _tabclick: function (e) {
    let item = e.detail;
    let urlStr = '';
    let params = {}
    if (item.name === 'profileList') {
      urlStr = `${proxy}/community/v2/user/profile`;
      params = {
        resourceType: '0,2',
        imgsize: 'p1,p2,p3',
        queriedUserId: this.data.userId,
        page: 1,
        size: 8,
        type: 'json'
      };
    } else if (item.name === 'all') {
      urlStr = `${proxy}/community/v2/user/myphoto`;
      params = {
        resourceType: '0,2',
        imgsize: 'p1,p2,p3',
        page: 1,
        size: 8,
        type: 'json'
      };
    } else if (item.name === 'imgAlbum') {
      urlStr = `${proxy}/community/user-details/${this.data.userId}/set`;
      params = {
        page: 1,
        size: 8,
        type: 'json'
      };
    }

    this.setData({
      urlStr: urlStr,
      params: params,
      curWorksType: item.name
    })
  },

  setTabs: function () {
    let tabEl = this.selectComponent('#worksTabs');
    let me = this;
    if (!tabEl) {
      return
    }
    let tabList = []
    let activeTab = {}
    if (LoginStore.userInfo.userId === this.data.userId) {
      tabList = [{
        displayName: '公开',
        name: 'profileList'
      }, {
        displayName: '全部',
        name: 'all'
      }, {
        displayName: '影集',
        name: 'imgAlbum'
      }]
      activeTab = tabList.filter((tab) => tab.name === me.data.curWorksType)[0];
      tabEl.setTabs(tabList, (item) => {
        this._tabclick({
          detail: {
            name: me.data.curWorksType
          }
        })
      }, activeTab)
    } else {
      tabList = [{
        displayName: '公开',
        name: 'profileList'
      }, {
        displayName: '影集',
        name: 'imgAlbum'
      }]
      activeTab = tabList.filter((tab) => tab.name === me.data.curWorksType)[0];
      tabEl.setTabs(tabList, (item) => {
        this._tabclick({
          detail: {
            name: me.data.curWorksType
          }
        })
      }, activeTab)
    }
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let params = this.data.params;
    params.page++;
    this.setData({
      params: params
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})